//
//  DetailViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "AppTools.h"
#import "pass.h"
#import "DataBaseSingleton.h"
#import "movieShouCang.h"
#import "LoigInViewController.h"

@interface MovieDetailViewController ()
@property(nonatomic,retain)UINavigationController *login;
@property(nonatomic,retain)MovieDetailView *myDetailView;
@property(nonatomic,retain)UIAlertController *alert;

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //---------------只用来设置 标题  title
    
    NSString *str = [pass share].movieID;
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",str];
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        NSMutableDictionary *resultDic = [dic objectForKeyedSubscript:@"result"];
        
        
        self.navigationItem.title = [resultDic objectForKey:@"title"];
        
    }];
    //-------------------------------------
    
    
    
    //正常铺设页面
    self.view.backgroundColor = [UIColor whiteColor];
    
    //加一个scorllview
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:scrollView];
    
    //设置scorllView 属性
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(375, 715);
    scrollView.bounces = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = YES;
    scrollView.contentOffset = CGPointMake(0, 0);
    
    
    
    
    self.myDetailView = [[MovieDetailView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [scrollView addSubview:self.myDetailView];
    [_myDetailView release];
    
    
    
    
    //设置左侧按钮
    UIImage *image = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftButton:)]autorelease];
    
    
    //设置电影收藏表
    UIImage *image1 = [UIImage imageNamed:@"btn_nav_share@2x.png"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image1 style:UIBarButtonItemStylePlain target:self action:@selector(rightButton:)]autorelease];
    
    
    
}


//左侧点击事件
-(void)leftButton:(UIBarButtonItem *)btn
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}


//右侧电影收藏按钮

-(void)rightButton:(UIBarButtonItem*)btn
{
    //开数据库
    [[DataBaseSingleton shareDataBase]openDB];
    
    NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
    resultDic = [pass share].movieDtailDic;
    NSMutableArray *nameArray = [NSMutableArray array];
    
    
    NSString *moviename = [resultDic objectForKey:@"title"];
    NSString *rat = [resultDic objectForKey:@"rating"];
    NSString *rating_count = [resultDic objectForKey:@"rating_count"];
    NSUserDefaults *uerDefault = [NSUserDefaults standardUserDefaults];
    NSString *own = [uerDefault objectForKey:@"name"];
    
    
    
    
    
    
    
    NSMutableArray *allShoucangArray = [NSMutableArray array];
    
    if (own.length == 0) {
      //  未登录状态
        UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"你还没有登录,请登录" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
        [actionsheet showInView:self.view.window];
        [actionsheet release];
        LoigInViewController *loginVC = [[LoigInViewController alloc]init];
        self.login = [[UINavigationController alloc]initWithRootViewController:loginVC];
        
        self.login.navigationBar.backgroundColor = [UIColor colorWithRed:0.980 green:0.613 blue:0.948 alpha:1.000];
        
        
        [self.login setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:self.login animated:YES completion:^{
            
            
        }];

        
        
        
        
        
    }
    //登录状态
    else
    {
        
        allShoucangArray =  [[DataBaseSingleton shareDataBase]selectAllShouCang];
        
        for (movieShouCang *moviecang in allShoucangArray)
        {
            if ([moviecang.person isEqualToString:own])
            {
                [nameArray addObject:moviecang.movieName];
            }
        }
        //如果收藏人 收藏此电影
        if ([nameArray containsObject:moviename])
        {
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"你已经收藏过了" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
            [actionsheet showInView:self.view.window];
            [actionsheet release];
            
        }
        //当登录人未收藏此电影  把此电影信息 收藏到收藏表
        else
        {
            
            movieShouCang * msc = [[movieShouCang alloc]init];
            msc.movieName = moviename;
            msc.rating = rating_count;
            msc.score = rat;
            msc.person = own;
            
            //建表
            
            [[DataBaseSingleton shareDataBase]createShouCangTable];
            
            
            //插入数据
            [[DataBaseSingleton shareDataBase]insertShouCang:msc];
            
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"收藏成功" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
            [actionsheet showInView:self.view.window];
            [actionsheet release];
           
            
            
        }
        
        
    }
    
    
    
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
